<template>
    <div>
        <div v-if="config.dimension.length > 0">
            <SubDimension v-for="(item, index) in config.dimension" :key="index" v-bind:events="selectedEvents()" v-bind:properties="properties" v-bind:dimension="config.dimension" v-bind:index="index"></SubDimension>
        </div>
        <a-button v-if="config.dimension.length === 0" @click="addItem()">添加细分维度</a-button>
    </div>
</template>

<script>
    import SubDimension from './SubDimension'
    export default {
        name: "SubDimensionGroup",
        components: {
            SubDimension
        },
        props: {
            config: Object,
            events: Array,
            properties: Array,
        },
        data: function () {
            return {

            };
        },
        methods: {
            selectedEvents: function () {
                return this.events.filter( event => {
                    return this.config.events.indexOf(event.name) !== -1;
                });
            },
            dimensionEvents: function() {
                let dimensionEvents = [];
                for(let i = 0; i < this.config.dimension.length; i++) {
                    let tokens = this.config.dimension[i].split(".");
                    if(tokens.length === 3) {
                        dimensionEvents.push(tokens[1]);
                    }
                }
                return dimensionEvents;
            },
            addItem: function () {
                let firstEvent = "";
                let dimensionEvents = this.dimensionEvents();
                for(let i = 0; i < this.config.events.length; i++) {
                    if(dimensionEvents.indexOf(this.config.events[i]) === -1) {
                        firstEvent = this.config.events[i];
                    }
                }
                let firstProperty = this.properties.length > 0 ? this.properties[0].name : "";
                this.config.dimension.push("events."+firstEvent+"."+firstProperty);
            },
            removeItem: function(index) {
                this.config.dimension.splice(index, 1);
            }

        }

    }
</script>

<style scoped>

</style>